﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using EpiNEXT.Core;
using EpiNEXT.Core.Fields;
using EpiNEXT.Data;

namespace EpiNEXT.Core.Legacy
{
    public class EI7ProjectTemplateConverter
    {
        private XDocument TemplateDocument { get; set; }
        private const double FONT_SIZE_OFFSET = 3;

        public EI7ProjectTemplateConverter(string fileName)
        {
            XDocument templateDocument = XDocument.Load(fileName);
            this.TemplateDocument = templateDocument;
        }

        public bool IsValidEpiInfo7Template()
        {
            // TODO: Check if it's a valid template first
            return true;
        }

        public Project Convert()
        {
            Project project = new Project();

            XElement projectElement = (from xml in TemplateDocument.Descendants("Project")
                                select xml).FirstOrDefault();

            project.Title = projectElement.Attribute("Name").Value;
            project.DataDriverString = "EpiNEXT.Data.XmlDriver";
            project.DataLocation = "Self";

            DataDriverFactory dataDriverFactory = new DataDriverFactory();
            IDbDriver database = dataDriverFactory.CreateDataDriver(project.DataDriverString, project.DataLocation);
            project.Database = database;

            var codeLists = (from xml in TemplateDocument.Descendants("SourceTable")
                         select xml).ToList();

            foreach (XElement codeListElement in codeLists)
            {
                CodeList list = new CodeList();
                list.Name = codeListElement.Attribute("TableName").Value;

                var items = (from xml in codeListElement.Descendants("Item")
                             select xml).ToList();

                foreach (XElement itemElement in items)
                {
                    CodeListValue listValue = new CodeListValue();

                    IEnumerable<XAttribute> itemAttributes = itemElement.Attributes();
                    foreach (XAttribute attribute in itemAttributes)
                    {
                        string attributeName = attribute.Name.LocalName;
                        string attributeValue = attribute.Value;

                        listValue.Owner = list;
                        listValue.IsOtherValue = false;
                        listValue.Store = attributeValue;
                        listValue.Display = attributeValue;
                        listValue.Culture = String.Empty;

                        list.CodeListValues.Add(listValue);
                        continue; // TODO: Look into how to bring in additional columns... this doesn't do that right now
                    }
                }

                project.CodeLists.Add(list);
            }

            var forms = (from xml in TemplateDocument.Descendants("View")
                         select xml).ToList();

            foreach (XElement formElement in forms)
            {
                Form form = new Form(project);

                project.Forms.Add(form);
                form.Title = formElement.Attribute("Name").Value;
                form.IsRelatedForm = bool.Parse(formElement.Attribute("IsRelatedView").Value);

                var pages = (from xml in formElement.Descendants("Page")
                             select xml).ToList();

                foreach (XElement pageElement in pages)
                {
                    Page page = new Page(form);

                    form.Pages.Add(page);
                    page.Title = pageElement.Attribute("Name").Value;
                    page.Position = int.Parse(pageElement.Attribute("Position").Value);

                    var fields = (from xml in pageElement.Descendants("Field")
                                  select xml).ToList();

                    foreach (XElement fieldElement in fields)
                    {
                        int fieldTypeId = int.Parse(fieldElement.Attribute("FieldTypeId").Value);

                        switch (fieldTypeId)
                        {
                            case 1:
                            case 3:
                            case 4:
                            case 6:
                                TextBoxField tboxField = CreateTextBoxField(fieldElement, form, page, fieldTypeId);
                                form.Fields.Add(tboxField);
                                page.Fields.Add(tboxField);
                                form.FieldsDTO.TextBoxFields.Add(tboxField);
                                break;
                            case 2:
                                LabelField labelField = CreateLabelField(fieldElement, form, page);
                                form.Fields.Add(labelField);
                                page.Fields.Add(labelField);
                                form.FieldsDTO.LabelFields.Add(labelField);
                                break;
                            case 5:
                                NumberField numberField = CreateNumberField(fieldElement, form, page);
                                form.Fields.Add(numberField);
                                page.Fields.Add(numberField);
                                form.FieldsDTO.NumberFields.Add(numberField);
                                break;
                            case 10:
                                CheckBoxField checkBoxField = CreateCheckBoxField(fieldElement, form, page);
                                form.Fields.Add(checkBoxField);
                                page.Fields.Add(checkBoxField);
                                form.FieldsDTO.CheckBoxFields.Add(checkBoxField);
                                break;
                            case 17:
                            case 19:
                                ComboBoxField lvField = CreateComboBoxField(fieldElement, form, page, fieldTypeId);
                                form.Fields.Add(lvField);
                                page.Fields.Add(lvField);
                                form.FieldsDTO.ComboBoxFields.Add(lvField);
                                break;
                            case 11:
                                YesNoField ynField = CreateYesNoField(fieldElement, form, page);
                                form.Fields.Add(ynField);
                                page.Fields.Add(ynField);
                                form.FieldsDTO.YesNoFields.Add(ynField);
                                break;
                            case 21:
                                GroupField groupField = CreateGroupField(fieldElement, form, page);
                                form.Fields.Add(groupField);
                                page.Fields.Add(groupField);
                                form.FieldsDTO.GroupFields.Add(groupField);
                                break;
                            case 7:
                            case 8:
                            case 9:
                                DateBoxField dboxField = CreateDateBoxField(fieldElement, form, page);
                                dboxField.DateTimeType = DateTimeType.Date;
                                form.Fields.Add(dboxField);
                                page.Fields.Add(dboxField);
                                form.FieldsDTO.DateBoxFields.Add(dboxField);

                                if (fieldTypeId == 8)
                                {
                                    dboxField.DateTimeType = DateTimeType.Time;
                                }
                                if (fieldTypeId == 9)
                                {
                                    dboxField.DateTimeType = DateTimeType.DateTime;
                                }
                                break;
                        }
                    }
                }
            }

            project.InitalizeRecordCollectionSynchronization();
            return project;
        }

        private TextBoxField CreateTextBoxField(XElement fieldElement, Form form, Page page, int fieldTypeId = 1)
        {
            TextBoxField textBoxField = new TextBoxField();
            textBoxField.Form = form;
            textBoxField.Page = page;
            textBoxField.Name = fieldElement.Attribute("Name").Value;
            textBoxField.PageId = page.PageId;

            double controlTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double controlLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double controlHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double controlWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double controlTopPosition = controlTopPositionPercentage * 940;
            double controlLeftPosition = controlLeftPositionPercentage * 730;

            double controlHeight = controlHeightPercentage * 940;
            double controlWidth = controlWidthPercentage * 730;

            textBoxField.InputTop = controlTopPosition;
            textBoxField.InputLeft = controlLeftPosition;
            textBoxField.InputWidth = controlWidth;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("PromptTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("PromptLeftPositionPercentage").Value);
            //double promptHeightPercentage = double.Parse(fieldElement.Attribute("PromptHeightPercentage").Value);
            //double promptWidthPercentage = double.Parse(fieldElement.Attribute("PromptWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            //double promptHeight = promptHeightPercentage * 940;
            //double promptWidth = promptWidthPercentage * 730;

            textBoxField.PromptTop = promptTopPosition;
            textBoxField.PromptLeft = promptLeftPosition;
            textBoxField.PromptWidth = Double.NaN; // promptWidth;
            textBoxField.PromptHeight = Double.NaN; //promptHeight;

            textBoxField.InputFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            textBoxField.InputFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;

            textBoxField.PromptFontFamily = fieldElement.Attribute("PromptFontFamily").Value;
            textBoxField.PromptFontSize = double.Parse(fieldElement.Attribute("PromptFontSize").Value) + FONT_SIZE_OFFSET;
            textBoxField.PromptText = fieldElement.Attribute("PromptText").Value;
            textBoxField.IsRequired = bool.Parse(fieldElement.Attribute("IsRequired").Value);
            textBoxField.IsReadOnly = bool.Parse(fieldElement.Attribute("IsReadOnly").Value);

            if (fieldTypeId == 1 || fieldTypeId == 3)
            {
                textBoxField.MaxLength = int.Parse(fieldElement.Attribute("MaxLength").Value);
            }
            if (fieldTypeId == 3)
            {
                textBoxField.ForceUppercase = true;
            }
            if (fieldTypeId == 4)
            {
                textBoxField.AcceptsReturn = true;
                textBoxField.InputHeight = controlHeight;
            }
            textBoxField.TabIndex = (page.Position * 1000) + int.Parse(fieldElement.Attribute("TabIndex").Value);
            textBoxField.IsTabStop = bool.Parse(fieldElement.Attribute("HasTabStop").Value);

            return textBoxField;
        }

        private DateBoxField CreateDateBoxField(XElement fieldElement, Form form, Page page)
        {
            DateBoxField field = new DateBoxField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;

            double controlTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double controlLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double controlHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double controlWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double controlTopPosition = controlTopPositionPercentage * 940;
            double controlLeftPosition = controlLeftPositionPercentage * 730;

            double controlHeight = controlHeightPercentage * 940;
            double controlWidth = controlWidthPercentage * 730;

            field.InputTop = controlTopPosition;
            field.InputLeft = controlLeftPosition;
            field.InputWidth = controlWidth;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("PromptTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("PromptLeftPositionPercentage").Value);
            //double promptHeightPercentage = double.Parse(fieldElement.Attribute("PromptHeightPercentage").Value);
            //double promptWidthPercentage = double.Parse(fieldElement.Attribute("PromptWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            //double promptHeight = promptHeightPercentage * 940;
            //double promptWidth = promptWidthPercentage * 730;

            field.PromptTop = promptTopPosition;
            field.PromptLeft = promptLeftPosition;
            field.PromptWidth = Double.NaN; // promptWidth;
            field.PromptHeight = Double.NaN; //promptHeight;

            field.InputFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            field.InputFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;

            field.PromptFontFamily = fieldElement.Attribute("PromptFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("PromptFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.IsRequired = bool.Parse(fieldElement.Attribute("IsRequired").Value);
            field.IsReadOnly = bool.Parse(fieldElement.Attribute("IsReadOnly").Value);
            field.TabIndex = (page.Position * 1000) + int.Parse(fieldElement.Attribute("TabIndex").Value);
            field.IsTabStop = bool.Parse(fieldElement.Attribute("HasTabStop").Value);

            if (!String.IsNullOrEmpty(fieldElement.Attribute("Lower").Value))
            {
                DateTime lower = DateTime.Now;

                bool success = DateTime.TryParse(fieldElement.Attribute("Lower").Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out lower);
                if (success)
                {
                    field.LowerBound = lower.Ticks;
                }
            }

            if (!String.IsNullOrEmpty(fieldElement.Attribute("Upper").Value))
            {
                DateTime upper = DateTime.Now;

                bool success = DateTime.TryParse(fieldElement.Attribute("Upper").Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out upper);
                if (success)
                {
                    field.UpperBound = upper.Ticks;
                }
            }

            field.FormatMode = DateFormatMode.Default;

            return field;
        }

        private ComboBoxField CreateComboBoxField(XElement fieldElement, Form form, Page page, int type = 17)
        {
            ComboBoxField field = new ComboBoxField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;

            double controlTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double controlLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double controlHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double controlWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double controlTopPosition = controlTopPositionPercentage * 940;
            double controlLeftPosition = controlLeftPositionPercentage * 730;

            double controlHeight = controlHeightPercentage * 940;
            double controlWidth = controlWidthPercentage * 730;

            field.InputTop = controlTopPosition;
            field.InputLeft = controlLeftPosition;
            field.InputWidth = controlWidth;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("PromptTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("PromptLeftPositionPercentage").Value);
            //double promptHeightPercentage = double.Parse(fieldElement.Attribute("PromptHeightPercentage").Value);
            //double promptWidthPercentage = double.Parse(fieldElement.Attribute("PromptWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            //double promptHeight = promptHeightPercentage * 940;
            //double promptWidth = promptWidthPercentage * 730;

            field.PromptTop = promptTopPosition;
            field.PromptLeft = promptLeftPosition;
            field.PromptWidth = Double.NaN; // promptWidth;
            field.PromptHeight = Double.NaN; //promptHeight;

            field.InputFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            field.InputFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;

            field.PromptFontFamily = fieldElement.Attribute("PromptFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("PromptFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.IsRequired = bool.Parse(fieldElement.Attribute("IsRequired").Value);
            field.IsReadOnly = bool.Parse(fieldElement.Attribute("IsReadOnly").Value);
            field.ListSourceName = fieldElement.Attribute("SourceTableName").Value;// +"_" + fieldElement.Attribute("TextColumnName").Value;
            field.TabIndex = (page.Position * 1000) + int.Parse(fieldElement.Attribute("TabIndex").Value);
            field.IsTabStop = bool.Parse(fieldElement.Attribute("HasTabStop").Value);

            foreach (CodeList list in form.Owner.CodeLists)
            {
                if (list.Name.Equals(field.ListSourceName, StringComparison.OrdinalIgnoreCase))
                {
                    field.ListSource = list;

                    if (type == 19)
                    {
                        // comment legal
                        list.Mode = CodeListMode.Coded;
                        list.Separator = "-";
                        list.Concatenate = true;

                        foreach (CodeListValue listValue in list.CodeListValues)
                        {
                            if (listValue.Store.Contains("-"))
                            {
                                string originalValue = listValue.Store;
                                string[] parts = originalValue.Split('-');

                                listValue.Store = parts[0];
                                listValue.Display = parts[1];
                            }
                        }
                    }
                    break;
                }
            }

            return field;
        }

        private YesNoField CreateYesNoField(XElement fieldElement, Form form, Page page)
        {
            YesNoField field = new YesNoField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;

            double controlTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double controlLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double controlHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double controlWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double controlTopPosition = controlTopPositionPercentage * 940;
            double controlLeftPosition = controlLeftPositionPercentage * 730;

            double controlHeight = controlHeightPercentage * 940;
            double controlWidth = controlWidthPercentage * 730;

            field.InputTop = controlTopPosition;
            field.InputLeft = controlLeftPosition;
            field.InputWidth = controlWidth;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("PromptTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("PromptLeftPositionPercentage").Value);
            //double promptHeightPercentage = double.Parse(fieldElement.Attribute("PromptHeightPercentage").Value);
            //double promptWidthPercentage = double.Parse(fieldElement.Attribute("PromptWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            //double promptHeight = promptHeightPercentage * 940;
            //double promptWidth = promptWidthPercentage * 730;

            field.PromptTop = promptTopPosition;
            field.PromptLeft = promptLeftPosition;
            field.PromptWidth = Double.NaN; // promptWidth;
            field.PromptHeight = Double.NaN; //promptHeight;

            field.InputFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            field.InputFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;

            field.PromptFontFamily = fieldElement.Attribute("PromptFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("PromptFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.IsRequired = bool.Parse(fieldElement.Attribute("IsRequired").Value);
            field.IsReadOnly = bool.Parse(fieldElement.Attribute("IsReadOnly").Value);
            field.TabIndex = (page.Position * 1000) + int.Parse(fieldElement.Attribute("TabIndex").Value);
            field.IsTabStop = bool.Parse(fieldElement.Attribute("HasTabStop").Value);
            field.IncludeUnknown = false;

            field.ListSource = field.Form.Owner.CodeListYN;

            return field;
        }

        private NumberField CreateNumberField(XElement fieldElement, Form form, Page page)
        {
            NumberField field = new NumberField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;

            double controlTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double controlLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double controlHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double controlWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double controlTopPosition = controlTopPositionPercentage * 940;
            double controlLeftPosition = controlLeftPositionPercentage * 730;

            double controlHeight = controlHeightPercentage * 940;
            double controlWidth = controlWidthPercentage * 730;

            field.InputTop = controlTopPosition;
            field.InputLeft = controlLeftPosition;
            field.InputWidth = controlWidth;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("PromptTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("PromptLeftPositionPercentage").Value);
            //double promptHeightPercentage = double.Parse(fieldElement.Attribute("PromptHeightPercentage").Value);
            //double promptWidthPercentage = double.Parse(fieldElement.Attribute("PromptWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            //double promptHeight = promptHeightPercentage * 940;
            //double promptWidth = promptWidthPercentage * 730;

            field.PromptTop = promptTopPosition;
            field.PromptLeft = promptLeftPosition;
            field.PromptWidth = Double.NaN; // promptWidth;
            field.PromptHeight = Double.NaN; //promptHeight;

            field.InputFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            field.InputFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;

            field.PromptFontFamily = fieldElement.Attribute("PromptFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("PromptFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.IsRequired = bool.Parse(fieldElement.Attribute("IsRequired").Value);
            field.IsReadOnly = bool.Parse(fieldElement.Attribute("IsReadOnly").Value);

            decimal lower;
            bool success = decimal.TryParse(fieldElement.Attribute("Lower").Value, out lower);
            if (success)
            {
                field.LowerBound = lower;
            }

            decimal upper;
            success = decimal.TryParse(fieldElement.Attribute("Upper").Value, out upper);
            if (success)
            {
                field.UpperBound = upper;
            }

            field.TabIndex = (page.Position * 1000) + int.Parse(fieldElement.Attribute("TabIndex").Value);
            field.IsTabStop = bool.Parse(fieldElement.Attribute("HasTabStop").Value);

            return field;
        }

        private LabelField CreateLabelField(XElement fieldElement, Form form, Page page)
        {
            LabelField field = new LabelField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;
            
            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double promptHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double promptWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            double promptHeight = promptHeightPercentage * 940;
            double promptWidth = promptWidthPercentage * 730;

            field.PromptTop = promptTopPosition;
            field.PromptLeft = promptLeftPosition;
            field.PromptWidth = promptWidth;
            field.PromptHeight = promptHeight;

            field.PromptFontWeight = fieldElement.Attribute("ControlFontStyle").Value;
            field.PromptFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.TabIndex = 0;
            field.IsTabStop = false;

            return field;
        }

        private GroupField CreateGroupField(XElement fieldElement, Form form, Page page)
        {
            GroupField field = new GroupField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double promptHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double promptWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            double promptHeight = promptHeightPercentage * 940;
            double promptWidth = promptWidthPercentage * 730;

            field.Height = promptHeight;
            field.Width = promptWidth;
            field.PromptTop = promptTopPosition;
            field.PromptLeft = promptLeftPosition;

            field.PromptFontWeight = fieldElement.Attribute("ControlFontStyle").Value;
            field.PromptFontFamily = fieldElement.Attribute("ControlFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("ControlFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.TabIndex = 0;
            field.IsTabStop = false;

            return field;
        }


        private CheckBoxField CreateCheckBoxField(XElement fieldElement, Form form, Page page)
        {
            CheckBoxField field = new CheckBoxField();
            field.Form = form;
            field.Page = page;
            field.Name = fieldElement.Attribute("Name").Value;
            field.PageId = page.PageId;

            double promptTopPositionPercentage = double.Parse(fieldElement.Attribute("ControlTopPositionPercentage").Value);
            double promptLeftPositionPercentage = double.Parse(fieldElement.Attribute("ControlLeftPositionPercentage").Value);
            double promptHeightPercentage = double.Parse(fieldElement.Attribute("ControlHeightPercentage").Value);
            double promptWidthPercentage = double.Parse(fieldElement.Attribute("ControlWidthPercentage").Value);

            double promptTopPosition = promptTopPositionPercentage * 940;
            double promptLeftPosition = promptLeftPositionPercentage * 730;

            double promptHeight = promptHeightPercentage * 940;
            double promptWidth = promptWidthPercentage * 730;

            field.InputTop = promptTopPosition;
            field.InputLeft = promptLeftPosition;
            field.InputWidth = promptWidth;
            field.InputHeight = promptHeight;

            field.PromptFontFamily = fieldElement.Attribute("PromptFontFamily").Value;
            field.PromptFontSize = double.Parse(fieldElement.Attribute("PromptFontSize").Value) + FONT_SIZE_OFFSET;
            field.PromptText = fieldElement.Attribute("PromptText").Value;
            field.IsRequired = bool.Parse(fieldElement.Attribute("IsRequired").Value);
            field.IsReadOnly = bool.Parse(fieldElement.Attribute("IsReadOnly").Value);
            field.TabIndex = (page.Position * 1000) + int.Parse(fieldElement.Attribute("TabIndex").Value);
            field.IsTabStop = bool.Parse(fieldElement.Attribute("HasTabStop").Value);

            return field;
        }
    }
}
